package co.yixiang.hw.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import co.yixiang.hw.domain.SupplierUserApply;
import co.yixiang.hw.repository.SupplierUserApplyRepository;
import co.yixiang.hw.service.SupplierUserApplyService;
import co.yixiang.hw.service.dto.SupplierUserApplyDto;
import co.yixiang.hw.service.dto.SupplierUserApplyQueryCriteria;
import co.yixiang.hw.service.mapper.SupplierUserApplyMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
* @author Walert
* @date 2021-03-27
*/
@Service
//@CacheConfig(cacheNames = "supplierUserApply")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SupplierUserApplyServiceImpl implements SupplierUserApplyService {

    @Resource
    private SupplierUserApplyRepository supplierUserApplyRepository;
    @Resource
    private SupplierUserApplyMapper supplierUserApplyMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(SupplierUserApplyQueryCriteria criteria, Pageable pageable){
        Page<SupplierUserApply> page = supplierUserApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(supplierUserApplyMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<SupplierUserApplyDto> queryAll(SupplierUserApplyQueryCriteria criteria){
        return supplierUserApplyMapper.toDto(supplierUserApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public SupplierUserApplyDto findById(Long id) {
        SupplierUserApply supplierUserApply = supplierUserApplyRepository.findById(id).orElseGet(SupplierUserApply::new);
        ValidationUtil.isNull(supplierUserApply.getId(),"SupplierUserApply","id",id);
        return supplierUserApplyMapper.toDto(supplierUserApply);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public SupplierUserApplyDto create(SupplierUserApply resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return supplierUserApplyMapper.toDto(supplierUserApplyRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(SupplierUserApply resources) {
        SupplierUserApply supplierUserApply = supplierUserApplyRepository.findById(resources.getId()).orElseGet(SupplierUserApply::new);
        ValidationUtil.isNull( supplierUserApply.getId(),"SupplierUserApply","id",resources.getId());
        supplierUserApply.copy(resources);
        supplierUserApplyRepository.save(supplierUserApply);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            supplierUserApplyRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SupplierUserApplyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SupplierUserApplyDto supplierUserApply : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("代理商ID", supplierUserApply.getUserId());
            map.put("代理商审核状态 1：申请中 2：审核通过 3：审核不通过", supplierUserApply.getAuditStatus());
            map.put("不通过原因", supplierUserApply.getNopassReason());
            map.put("姓名", supplierUserApply.getUserName());
            map.put("公司名称", supplierUserApply.getCompanyName());
            map.put("是否删除 0：否 1：是", supplierUserApply.getIsDel());
            map.put("创建时间", supplierUserApply.getCreateTime());
            map.put("更新时间", supplierUserApply.getUpdateTime());
            map.put("更新用户", supplierUserApply.getUpdateUser());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public SupplierUserApply getNewApplyByUserId(Long userId) {
        List<SupplierUserApply> list = this.supplierUserApplyRepository.findByUserIdAndIsDelOrderByIdDesc(userId, 0);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }
}
